package net.toocruel.iqismart.web.controller.home;

import net.toocruel.iqismart.entity.core.Exchange;
import net.toocruel.iqismart.entity.core.Good;
import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/14 下午2:12
 */
@Controller
@RequestMapping("shop")
public class HomeShopController extends AbstractBaseController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private GoodService goodService;



    @RequestMapping("")
    public ModelAndView questions(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestParam(defaultValue = "6") int size,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "id") String sortBy,
                                  @RequestParam(defaultValue = "desc") String sortOrder,
                                  @RequestParam(defaultValue = "newest") String filter){
        //
        ModelAndView modelAndView = new ModelAndView(getTemplate("shop"));



        Page<Good> goods = goodService.findAll(new PageRequest(page-1, size,new Sort(Sort.Direction.fromString(sortOrder),sortBy)));

        List<Exchange> exchanges = goodService.getExchanges(8);

        modelAndView.addObject("goods",goods);
        modelAndView.addObject("exchanges",exchanges);

        //seoTitle
        String seoTitle = "商城 - "+Setting.SETTING.getWebsite().getName();
        modelAndView.addObject("seoTitle",seoTitle);

        refreshCurrentUser();

        return modelAndView;
    }





    @Override
    protected String getModule() {
        return "home";
    }

}
